package com.kexilo.system.audit.controller;



import com.kexilo.core.audit.domain.SysOperLog;
import com.kexilo.core.common.annotation.Debounce;
import com.kexilo.core.common.annotation.Idempotent;
import com.kexilo.core.common.annotation.Log;
import com.kexilo.core.common.core.page.TableDataInfo;
import com.kexilo.core.common.core.web.BaseController;
import com.kexilo.core.common.core.web.domain.R;
import com.kexilo.core.common.enums.BusinessType;
import com.kexilo.core.common.utils.ExcelUtil;
import com.kexilo.system.audit.service.ISysOperLogService;
import jakarta.servlet.http.HttpServletResponse;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 操作日志记录
 * 
 * @author Kexilo
 */
@RestController
@RequestMapping("/monitor/operlog")
@Tag(name = "操作日志管理", description = "操作日志监控相关接口")
public class SysOperlogController extends BaseController {
    
    private static final Logger log = LoggerFactory.getLogger(SysOperlogController.class);
    
    @Autowired
    private ISysOperLogService operLogService;

    @PreAuthorize("@ss.hasPermi('monitor:operlog:list')")
    @GetMapping("/list")
    @Operation(summary = "获取操作日志列表", description = "分页查询操作日志信息")
    @ApiResponses({
        @ApiResponse(responseCode = "200", description = "获取成功"),
        @ApiResponse(responseCode = "403", description = "权限不足")
    })
    public TableDataInfo list(SysOperLog operLog) {
        startPage();
        List<SysOperLog> list = operLogService.selectOperLogList(operLog);
        return getDataTable(list);
    }

    @Debounce(interval = 10, message = "导出操作过于频繁，请稍后再试")
    @Log(title = "操作日志", businessType = BusinessType.EXPORT)
    @PreAuthorize("@ss.hasPermi('monitor:operlog:export')")
    @PostMapping("/export")
    @Operation(summary = "导出操作日志", description = "导出操作日志数据")
    public void export(SysOperLog operLog, HttpServletResponse response) {
        try {
            List<SysOperLog> list = operLogService.selectOperLogList(operLog);
            ExcelUtil<SysOperLog> util = new ExcelUtil<>(SysOperLog.class);
            util.exportExcel(list, "操作日志", response);
        } catch (Exception e) {
            throw new RuntimeException("导出失败: " + e.getMessage());
        }
    }

    @PreAuthorize("@ss.hasPermi('monitor:operlog:query')")
    @GetMapping("/{operId}")
    @Operation(summary = "获取操作日志详情", description = "根据操作日志ID获取详细信息")
    public R<SysOperLog> getInfo(@Parameter(description = "操作日志ID") @PathVariable Long operId) {
        return success(operLogService.selectOperLogById(operId));
    }

    @PreAuthorize("@ss.hasPermi('monitor:operlog:remove')")
    @Log(title = "操作日志", businessType = BusinessType.DELETE)
    @Debounce(strategy = Debounce.Strategy.USER_METHOD, interval = 5, message = "删除操作过于频繁，请稍后再试")
    @Idempotent(strategy = Idempotent.Strategy.REQUEST_PARAMS, timeout = 5, delKey = true, message = "删除操作正在进行中，请勿重复操作")
    @DeleteMapping("/{operIds}")
    @Operation(summary = "删除操作日志", description = "批量删除操作日志")
    public R<Void> remove(@Parameter(description = "操作日志ID数组") @PathVariable Long[] operIds) {
        return toAjax(operLogService.deleteOperLogByIds(operIds));
    }

    @PreAuthorize("@ss.hasPermi('monitor:operlog:remove')")
    @Log(title = "操作日志", businessType = BusinessType.CLEAN)
    @Debounce(strategy = Debounce.Strategy.USER_METHOD, interval = 30, message = "清空操作过于频繁，请稍后再试")
    @Idempotent(strategy = Idempotent.Strategy.USER_BASED, timeout = 10, message = "清空操作正在进行中，请稍后再试")
    @DeleteMapping("/clean")
    @Operation(summary = "清空操作日志", description = "清空所有操作日志")
    public R<Void> clean() {
        operLogService.cleanOperLog();
        return success();
    }
}
